<template>
  <div>
    <el-collapse-transition>
      <el-card v-if="searchShow" shadow="never" :body-style="{ padding: '20px 10px 0px 20px' }">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-form-item>
            <el-input v-model="dataForm.type" :placeholder="$t('wechat.type')" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="dataForm.templateId" placeholder="templateId" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()" type="primary">{{ $t('query') }}</el-button>
            <el-button @click="searchReset()">{{ $t('reset') }}</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-collapse-transition>
    <el-card shadow="never" class="aui-card--fill" :style="searchShow?'margin-top: 10px;':''">
      <div class="crud__menu">
        <div class="crud__left">
          <el-form :inline="true">
            <el-form-item>
              <el-button @click="refresh()" type="warning">{{ $t('wechat.refreshTile') }}</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="crud__right">
          <el-button-group>
            <el-button icon="el-icon-search" @click="searchShowHandle()"></el-button>
            <el-button icon="el-icon-refresh" @click="getDataList()"></el-button>
          </el-button-group>
        </div>
      </div>

      <div class="mod-sys__wechattemplate}">
        <el-table v-loading="dataListLoading" :data="dataList" tooltip-effect="dark" border
          @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
          <el-table-column type="index" width="50" fixed="left"></el-table-column>
          <el-table-column prop="type" :label="$t('wechat.type')" header-align="center" align="center" width="150"
            show-overflow-tooltip></el-table-column>
          <el-table-column prop="title" :label="$t('wechat.title')" header-align="center" align="left" width="150"
            show-overflow-tooltip></el-table-column>
          <el-table-column prop="primaryIndustry" :label="$t('wechat.primaryIndustry')" header-align="center"
            align="center" width="150" show-overflow-tooltip></el-table-column>
          <el-table-column prop="deputyIndustry" :label="$t('wechat.deputyIndustry')" header-align="center" align="left"
            width="150" show-overflow-tooltip></el-table-column>
          <el-table-column prop="example" :label="$t('wechat.example')" header-align="center" align="left"
            show-overflow-tooltip></el-table-column>
          <el-table-column :label="$t('wechat.status')" header-align="center" align="center" width="120">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.type == null" class="el-icon-close" size="small" type="danger">
                {{ $t('wechat.status0') }}</el-tag>
              <el-tag v-else size="small" class="el-icon-check" type="success">{{ $t('wechat.status1') }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column :label="$t('handle')" header-align="center" align="center" width="150">
            <template slot-scope="scope">
              <el-button v-if="$hasPermission('wechat:template:update')" icon="el-icon-edit" type="text" size="small"
                @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
              <el-button v-if="scope.row.type != null" type="text" icon="el-icon-news" size="small"
                @click="sendHandle(scope.row.id)">{{ $t('wechat.send') }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
          layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
        <send v-if="sendVisible" ref="send" @refreshDataList="getDataList"></send>
      </div>
    </el-card>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './template-add-or-update'
import Send from './template-send'

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/wechat/template/page',
        getDataListIsPage: true,
        deleteURL: '/wechat/template',
        refreshURL: '/wechat/template/refresh',
        deleteIsBatch: true
      },
      dataForm: {
        id: ''
      },
      sendVisible: false
    }
  },
  methods: {
    refresh () {
      this.dataListLoading = true
      this.$http.get(
        this.mixinViewModuleOptions.refreshURL
      ).then(({ data: res }) => {
        this.dataListLoading = false
        if (res.code !== 0) {
          this.dataList = []
          this.total = 0
          return
        }
        this.getDataList()
      }).catch(() => {
        this.dataListLoading = false
      })
    },
    // 发送邮件
    sendHandle (id) {
      this.sendVisible = true
      this.$nextTick(() => {
        this.$refs.send.dataForm.id = id
        this.$refs.send.init()
      })
    }
  },
  components: {
    AddOrUpdate,
    Send
  }
}
</script>
